package com.chuanjiabao.wisdom.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import com.chuanjiabao.wisdom.MainActivity
import com.chuanjiabao.wisdom.R
import com.chuanjiabao.wisdom.adapter.GuideAdapter
import com.chuanjiabao.wisdom.bean.MediaBody
import com.chuanjiabao.wisdom.bean.MediaData
import com.chuanjiabao.wisdom.util.*
import com.chuanjiabao.wisdom.util.dialog.FirstHintDialog
import com.chuanjiabao.wisdom.util.dialog.WxHintDialog
import com.lzy.okgo.callback.StringCallback
import kotlinx.android.synthetic.main.activity_guide.*
import okhttp3.Call
import okhttp3.Response

class GuideActivity : AppCompatActivity() {

    private val list = ArrayList<MediaBody>()
    private var status = -1
    @SuppressLint("HandlerLeak")
    private var handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (status > -1) {
                when (status) {
                    0 -> {
                        guide_out.visibility = View.VISIBLE
                        startGuide()
                    }
                    1 -> quite()
                    2 -> {
                        if (msg.what == 0) {
                            guide_out.visibility = View.VISIBLE
                            startGuide()
                        } else quite()
                    }
                }
            } else status = msg.what
        }
    }

    private fun startGuide() {
        val mLoopAdapter = GuideAdapter(this)
        guide_banner.apply {
            setAdapter(mLoopAdapter)
            setHintViewVisibility(false)
            setOnItemSelectListener { position ->
                mLoopAdapter.startAnim(position)
                guide_in.visibility = if (position == list.size - 1) View.VISIBLE else View.INVISIBLE
                if (position == list.size - 1) pause()
            }
        }

        mLoopAdapter.setOnDataDoneListener { mLoopAdapter.startAnim(0) }
        mLoopAdapter.setImgs(list)

        guide_banner.viewPager.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_MOVE) guide_banner.pause()
            return@setOnTouchListener false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //隐藏状态栏（全屏）
        window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)

        setContentView(R.layout.activity_guide)

        guide_out.setOnClickListener { quite() }
        guide_in.setOnClickListener { quite() }

        val isFirst = SpUtils.get("first_open", true) as Boolean

        if (isFirst) {
            FirstHintDialog(this)
                    .apply {
                        setOnBHClickListener {
                            when (it) {
                                1 -> {
                                    WxHintDialog(this@GuideActivity)
                                            .apply {
                                                setContent("http://home.zzwisdom.com/deal.html")
                                                setTitleHint("用户协议")
                                                setBtnHint("确定")
                                                setOneBtn()
                                                show()
                                            }
                                }
                                2 -> {
                                    WxHintDialog(this@GuideActivity)
                                            .apply {
                                                setContent("https://home.zzwisdom.com/private.html")
                                                setTitleHint("隐私政策")
                                                setBtnHint("确定")
                                                setOneBtn()
                                                show()
                                            }
                                }
                                3 -> {
                                    SpUtils.put("first_open", false)
                                    status = 2
                                    getMedia()
                                }
                                4 -> finish()
                            }
                        }
                    }.show()
        } else {
            window.decorView.postDelayed({
                handler.sendEmptyMessage(2)
            }, 2000)

            getMedia()
        }
    }

    private fun quite() {
        val isLogin = SpResourceUtil.getIsLogin()
        if (isLogin.isEmpty()) {
            startActivity(Intent(this, LoginActivity::class.java))
        } else {
            startActivity(Intent(this, MainActivity::class.java))
        }
        onBackPressed()
    }

    private fun getMedia() {
        HTTPAPI.getInstance().getMedia(object : StringCallback() {
            override fun onSuccess(s: String, call: Call, response: Response) {
                if (StringUtil.isFail(s)) {
                    val data = GsonUtil.GsonToBean(s, MediaData::class.java)
                    list.addAll(data.data.body)

                    if (list.isEmpty()) handler.sendEmptyMessage(1)
                    else {
                        val spId = SpUtils.get("mediaId", -1) as Int
                        val spType = SpUtils.get("mediaType", -1) as Int

                        val mediaId = data.data.id
                        val mediaType = data.data.type
                        val mediaShow = data.data.always_show

                        if (mediaShow == 1) handler.sendEmptyMessage(0)
                        else {
                            if (mediaId != spId || mediaType != spType) {
                                handler.sendEmptyMessage(0)
                            } else {
                                handler.sendEmptyMessage(1)
                            }
                        }

                        SpUtils.put("mediaId", mediaId)
                        SpUtils.put("mediaType", mediaType)
                    }
                } else handler.sendEmptyMessage(1)
            }
        })
    }

}
